Conditionals
Video
Dictionary of Terms
- Assignment
- Equality
- Conditional control structures
- Boolean
if
statementif else
statementif elif else
statementor
and
not
conditions- modulo operator
- match case (switch case)
Conditionals
Conditionals allow you, the programmer, to allow your program to make decisions: As if your program has the choice between taking the left-hand road or the right-hand road based upon certain conditions. Built within Python are a set of “operators” that can are used to ask mathematical questions.
>
and<
symbols are probably quite familiar to you.>=
denotes “greater than or equal to.”<=
denotes “less than or equal to.”==
denotes “equals, though do notice the double equal sign! A single equal sign would assign a value. Double equal signs are used to compare variables.!=
denotes “not equal to.
Conditional statements compare a left-hand term to a right-hand term.
if Statements
- In your terminal window, type code
compare.py
. This will create a brand new file called “compare.” -
In the text editor window, begin with the following:
Notice how your program takes the input of the user for both x and y, casting them as integers and saving them into their respective x and y variables. Then, theif
statement compares x and y. If the condition ofx < y
is met, theprint
statement is executed. -
If statements use
bool
or boolean values (true or false) to decide whether or not to execute. If the statement ofx > y
is true, the compiler will register it astrue
and execute the code.
Control Flow: elif, and else
-
Further revise your code as follows:
Notice how you are providing a series ofx = int(input("What's x? ")) y = int(input("What's y? ")) if x < y: print("x is less than y") if x > y: print("x is greater than y") if x == y: print("x is equal to y")
if
statements. First, the firstif
statement is evaluated. Then, the secondif
statement runs its evaluation. Finally, the lastif
statement runs its evaluation. This flow of decisions is called “control flow.” -
Our code can be represented as follows:
flowchart TD id1([Start]) id2([Stop]) id1 --> id3{x < y} id3 -- True --> id4("x is less than y") id3 -- False --> id5{x > y} id4 --> id5 id5 -- True --> id6("x is greater than y") id5 -- False --> id7{x == y} id6 --> id7 id7 -- True --> id8("x is equal to y") id7 -- False --> id2 id8 --> id2
-
This program can be improved by not asking three consecutive questions. After all, not all three questions can have an outcome of true! Revise your program as follows:
Notice how the use ofx = int(input("What's x? ")) y = int(input("What's y? ")) if x < y: print("x is less than y") elif x > y: print("x is greater than y") elif x == y: print("x is equal to y")
elif
allows the program to make less decisions. First, theif
statement is evaluated. If this statement is found to be true, all theelif
statements not be run at all. However, if theif
statement is evaluated and found to be false, the firstelif
will be evaluated. If this is true, it will not run the final evaluation.
Our code can be represented as follows:
flowchart TD
id1([Start])
id2([Stop])
id1 --> id3{x < y}
id3 -- True --> id4("x is less than y")
id4 --> id2
id3 -- False --> id5{x > y}
id5 -- True --> id6("x is greater than y")
id6 --> id2
id5 -- False --> id7{x == y}
id7 -- True --> id8("x is equal to y")
id8 --> id2
id7 -- False --> id2
- While your computer may not notice a difference speed-wise between our first program and this revised program, consider how an online server running billions or trillions of these types of calculations each day could definitely be impacted by such a small coding decision.
- There is one final improvement we can make to our program. Notice how logically
elif x == y
is not a necessary evaluation to run. After all, if logically x is not less than y AND x is not greater than y, x MUST equal y. Therefore, we don’t have to run elifx == y
. We can create a “catch-all,” default outcome using anelse
statement. We can revise as follows: Notice how the relative complexity of this program has decreased through our revision.
Our code can be represented as follows:
flowchart TD
id1([Start])
id2([Stop])
id1 --> id3[x = read input]
id3 --> id4[y = read input]
id4 --> id5{x < y}
id5 -- True --> id6("x is less than y")
id6 --> id2
id5 -- False --> id7{x > y}
id7 -- True --> id8("x is greater than y")
id8 --> id2
id7 -- False --> id9("x is equal to y")
id9 --> id2
Operators
or
-
Notice that the result of our program is the same, but the complexity is decreased and the efficiency of our code is increased.or
allows your program to decide between one or more alternatives. For example, we could further edit our program as follows: -
At this point, our code is pretty great. However, could the design be further improved? We could further edit our code as follows:
Notice how we removed thex = int(input("What's x? ")) y = int(input("What's y? ")) if x != y: print("x is not equal to y") else: print("x is equal to y")
or
entirely, and simply asked “is x not equal to y?” We ask one and only one question. Very efficient! -
For the purpose of illustration, we could also change our code as follows:
Notice that thex = int(input("What's x? ")) y = int(input("What's y? ")) if x == y: print("x is equal to y") else: print("x is not equal to y")
==
operator evaluates if what is on the left and right are equal to one another. That use of double equal signs (equality
) is very important. If you use only one equal sign, an error will be thrown by the compiler as it is being used as an assignment operator. -
Our code can be illustrated as follows:
flowchart TD
id1([Start])
id2([Stop])
id1 --> id3[x = read input]
id3 --> id4[y = read input]
id4 --> id5{x == y}
id5 -- True --> id6("x is equal to y")
id6 --> id2
id5 -- False --> id7(x is not equal to y)
id7 --> id2
and
- Similar to
or
,and
can be used within conditional statements. -
Execute in the terminal window
code grade.py
. Start your new program as follows:Notice that executingscore = int(input("Score: ")) if score >= 90 and score <= 100: print("Grade: A") elif score >=80 and score < 90: print("Grade: B") elif score >=70 and score < 80: print("Grade: C") elif score >=60 and score < 70: print("Grade: D") else: print("Grade: F")
python grade.py
you will be able to input a score and get a grade. However, notice how there are potentials for bugs. -
Typically, we do not want to ever trust our user to input the correct information. We could improve our code as follows:
Notice how Python allows you to chain together the operators and conditions. This is quite uncommon in other programming languages. -
Still, we can further improve our program:
Notice how the program is improved by checking few conditions. This makes our program easier to read and far more maintainable in the future. -
You can learn more in Python’s documentation on control flow.
Modulo %
- In mathematics, parity refers to whether a number is either even or odd.
- The modulo
%
operator in programming allows one to see if two numbers divide evenly or divide and have a remainder. - For example, 4 % 2 would result in zero, because it evenly divides. However, 3 % 2 does not divide evenly and would result in a number other than zero!
- In the terminal window, create a new program by typing
code parity.py
. In the text editor window, type your code as follows: Notice how our users can type in any integer to see if it is even or odd.
Creating Our Own Parity Function
- As discussed in Lecture 0, you will find it useful to create a function of your own!
We- can create our own function to check whether a number is even or odd. Adjust your code as follows:
Notice that one reason our
def main(): x = int(input("What's x? ")) if is_even(x): print("Even") else: print("Odd") def is_even(n): if n % 2 == 0: return True else: return False main()
if
statementis_even(x)
works, even though there is no operator there. This is because our function returns abool
(or boolean), true or false, back to the main function. Theif
statement simply evaluates whether or notis_even
ofx
is true or false.
Pythonic
-
In the programming world, there are types of programming that are called “Pythonic” in nature. That is, there are ways to program that are sometimes only seen in Python programming. Consider the following revision to our program:
Notice that this return statement in our code is almost like a sentence in English. This is a unique way of coding only seen in Python. -
We can further revise our code and make it more and more readable:
Notice that the program will evaluate what is happening within thedef main(): x = int(input("What's x? ")) if is_even(x): print("Even") else: print("Odd") def is_even(n): return n % 2 == 0 main()
n % 2 == 0
as either true or false and simply return that to the main function.
match Statement
Similar to if
, elif
, and else
statements, match statements can be used to conditionally run code that matches certain values.
Note
In other languages this would be called a case
statement.
-
Consider the following program:
Notice the first three conditional statements print the same response. -
We can improve this code slightly with the use of the
or
keyword:Notice the number ofname = input("What's your name? ") if name == "Harry" or name == "Hermione" or name == "Ron": print("Gryffindor") elif name == "Draco": print("Slytherin") else: print("Who?")
elif
statements has decreased, improving the readability of our code. -
Alternatively, we can use
match
statements to map names to houses. Consider the following code:Notice the use of thename = input("What's your name? ") match name: case "Harry": print("Gryffindor") case "Hermione": print("Gryffindor") case "Ron": print("Gryffindor") case "Draco": print("Slytherin") case _: print("Who?")
_
symbol in the last case. This will match with any input, resulting in similar behaviour as anelse
statement. -
A match statement compares the value following the
match
keyword with each of the values following thecase
keywords. In the event a match is found, the respective indented code section is executed and the program stops the matching. - We can improve the code:
Notice, the use of the single vertical bar
name = input("What's your name? ") match name: case "Harry" | "Hermione" | "Ron": print("Gryffindor") case "Draco": print("Slytherin") case _: print("Who?")
|
. Much like theor
keyword, this allows us to check for multiple values in the same case statement.
Summing Up
-
You now have the power within Python to use conditional statements to ask questions and have your program take action accordingly. In this lecture, we discussed…
-
Conditionals;
if
Statements;- Control flow,
elif
, andelse
; or
;and
;- Modulo;
- Creating your own function;
- Pythonic coding;
- and
match
.